package com.eos.inter.servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eos.common.tool.DaoAccessObjectPoolContextFactory;
import com.eos.orm.mybatis.dao.SpeeCustMonthReportMapper;
import com.eos.orm.mybatis.model.SpeeCustMonthReport;
import com.hf.utils.StringUtil;

/**
 * 专案客户每月报表（每月5日提供上月数据）接口
 * 
 * @author sz
 * 
 */
public class SpeeCustMonthReportServlet extends BaseHttpServlet {

	private static final long serialVersionUID = 1L;
	private SpeeCustMonthReportMapper mapper = (SpeeCustMonthReportMapper) DaoAccessObjectPoolContextFactory
			.getInstaAccessObjectPoolContext().instanceDataAccessObject(
					"SpeeCustMonthReportMapper");

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		SpeeCustMonthReport speeCustMonthReport = new SpeeCustMonthReport();// 添加数据的对象
		speeCustMonthReport.setCustomerId(StringUtil.parseInt(StringUtil
				.NullToString(request.getParameter("customer_id"))));// 区分哪个结算客户
		speeCustMonthReport.setPartnerName(StringUtil.NullToString(request
				.getParameter("parnter_name")));//
		speeCustMonthReport.setCustName(StringUtil.NullToString(request
				.getParameter("cust_name")));//
		speeCustMonthReport.setCustAlipayAccount(StringUtil
				.NullToString(request.getParameter("cust_alipay_account")));
		speeCustMonthReport.setCustIdCard(StringUtil.NullToString(request
				.getParameter("cust_id_card")));
		speeCustMonthReport.setCustAlipayAccountId(StringUtil
				.NullToString(request.getParameter("cust_alipay_account_id")));
		speeCustMonthReport.setTaobaoOrderId(StringUtil.NullToString(request
				.getParameter("taobao_order_id")));
		speeCustMonthReport.setCustContractPhone(StringUtil
				.NullToString(request.getParameter("cust_contract_phone")));
		speeCustMonthReport
				.setCustContractPhoneImei(StringUtil.NullToString(request
						.getParameter("cust_contract_phone_imei")));
		speeCustMonthReport.setPackageTotalAmount(StringUtil
				.NullToString(request.getParameter("package_total_amount")));
		speeCustMonthReport.setPackageType(StringUtil.NullToString(request
				.getParameter("package_type")));
		speeCustMonthReport.setPackageSingleAmount(StringUtil
				.NullToString(request.getParameter("package_single_amount")));
		speeCustMonthReport.setPackageStartDate(StringUtil.NullToString(request
				.getParameter("package_start_date")));
		speeCustMonthReport.setPackageEndDate(StringUtil.NullToString(request
				.getParameter("package_end_date")));
		speeCustMonthReport.setPackageRemainAmount(StringUtil
				.NullToString(request.getParameter("package_remain_amount")));
		speeCustMonthReport.setPackageRemainCount(StringUtil
				.NullToString(request.getParameter("package_remain_count")));
		speeCustMonthReport.setCustTotalConsumption(StringUtil
				.NullToString(request.getParameter("cust_total_consumption")));
		speeCustMonthReport.setCustTotalFee(StringUtil.NullToString(request
				.getParameter("cust_total_fee")));
		speeCustMonthReport.setCustTotalFeeNotfromAlipay(StringUtil
				.NullToString(request
						.getParameter("cust_total_fee_notfrom_alipay")));
		speeCustMonthReport.setCustCurConsumption(StringUtil
				.NullToString(request.getParameter("cust_cur_consumption")));
		speeCustMonthReport.setCustCurFee(StringUtil.NullToString(request
				.getParameter("cust_cur_fee")));
		speeCustMonthReport.setCustCurFeeNotformAlipay(StringUtil
				.NullToString(request
						.getParameter("cust_cur_fee_notfrom_alipay")));
		speeCustMonthReport.setCustGuaranteeAmount(StringUtil
				.NullToString(request.getParameter("cust_guarantee_amount")));
		speeCustMonthReport.setCustTraffic(StringUtil.NullToString(request
				.getParameter("cust_traffic")));
		speeCustMonthReport.setCustAddFee(StringUtil.NullToString(request
				.getParameter("cust_add_fee")));
		speeCustMonthReport.setReportMonth(StringUtil.NullToString(request
				.getParameter("report_month")));
		speeCustMonthReport.setInsertTime(StringUtil.stringDate(new Date()));
		speeCustMonthReport.setStatus("0");

		ServletOutputStream out = null;
		try {
			out = response.getOutputStream();
			if (identification(speeCustMonthReport.getCustContractPhone(),
					speeCustMonthReport.getCustIdCard())) {
				// 失败返回1
				out.write("1".toString().getBytes("utf-8"));
				return ;
			}
			if (0 != speeCustMonthReport.getCustomerId()
					&& mapper.insert(speeCustMonthReport) == 0) {
				// 成功返回0
				out.write("0".toString().getBytes("utf-8"));
			} else {
				// 失败返回1
				out.write("1".toString().getBytes("utf-8"));
			}
		} catch (Exception e) {
			log
					.error("com.eos.inter.servlet.SpecCustMonthReportServlet.doPost throws exception:"
							+ e.getMessage());
		} finally {
			if (null != out) {
				out.flush();
				out.close();
			}
		}
	}
}
